package com.vf.admin.client.service.impl;

import com.vf.admin.client.dao.AppMingXiSongLiDao;
import com.vf.admin.client.dao.AppSetupDao;
import com.vf.admin.client.dao.AppUserDao;
import com.vf.admin.client.dto.AppRankingDto;
import com.vf.admin.client.service.AppMingXiSongLiService;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppMingXiSongLi;
import com.vf.admin.miaodong.entity.AppSetup;
import com.vf.admin.miaodong.entity.AppUser;
import com.vf.admin.utils.AgeUtils;
import com.vf.admin.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.vf.admin.common.Constants.guizuCacheMap;

/**
 * @author anxiang
 * @version 1.0.0
 * @ClassName AppMingXiSongLiServiceImpl.java
 * @Description TODO
 * @createTime 2021年11月02日 15:21:00
 */
@Service
public class AppMingXiSongLiServiceImpl implements AppMingXiSongLiService {

    @Autowired
    AppMingXiSongLiDao appMingXiSongLiDao;
    @Autowired
    AppSetupDao appSetupDao;
    @Autowired
    AppUserDao appUserDao;

    @Override
    public void saveOrUpdate(AppMingXiSongLi appMingXiSongLi) {
        appMingXiSongLiDao.saveOrUpdate(appMingXiSongLi);
    }

    @Override
    public void updateGiftData(Integer giftid,String picture,String giftname) {
        appMingXiSongLiDao.updateGiftData(giftid,picture,giftname);
    }

    @Override
    public List<AppMingXiSongLi> findList(Integer userid, SplitPageBean splitPageBean) {
        return appMingXiSongLiDao.findList(userid, splitPageBean);
    }

    @Override
    public List<AppMingXiSongLi> list(Map<String, Object> params, SplitPageBean splitPageBean) {
        return appMingXiSongLiDao.list(params, splitPageBean);
    }

    @Override
    public Double listSum(Map<String, Object> paraMap) {
        List<AppMingXiSongLi> list = appMingXiSongLiDao.list(paraMap);
        Double sum = list.stream().mapToDouble(e -> e.getFujianum()).sum();
        return sum;
    }

    @Override
    public List<AppMingXiSongLi> leaderBoard(String time, String endtime) {
        return appMingXiSongLiDao.leaderBoard(time,endtime);
    }

    @Override
    public List<AppRankingDto> getRanking(Integer secondType, Map<String, Object> params) {

        List<AppMingXiSongLi> appMingXiSongLis = new ArrayList<>();
        ArrayList<AppRankingDto> appRankingDtos = new ArrayList<>();
        switch (secondType){
            case 1:
                appMingXiSongLis = appMingXiSongLiDao.getDayRanking(params);
                break;
            case 2:
                appMingXiSongLis = appMingXiSongLiDao.getWeekRanking(params);
                break;
            case 3:
                appMingXiSongLis = appMingXiSongLiDao.getMonthRanking(params);
                break;
            default:
                return null;
        }
        AppSetup setup = appSetupDao.get();
        if(appMingXiSongLis!=null && appMingXiSongLis.size()>0){
            appMingXiSongLis.forEach(e->{
                AppRankingDto appRankingDto = new AppRankingDto();
                AppUser appUser = appUserDao.get(e.getUserid());
                if(appUser != null) {
                    appRankingDto.setPic(appUser.getPic());
                    appRankingDto.setNick(appUser.getNick());
                    appRankingDto.setUserId(appUser.getId());
                    BigDecimal getMoney = e.getTotalMoney().multiply(setup.getJinDouExchangeForDiamonds()).setScale(2, BigDecimal.ROUND_HALF_UP);
                    appRankingDto.setTotalMoney(getMoney);
                    appRankingDto.setSex(appUser.getSex());
                    appRankingDto.setMySign(appUser.getMysign());
                    appRankingDto.setVoiceSignature(appUser.getVoiceSignature());
                    appRankingDto.setYearOld(AgeUtils.getAgeByBirth(appUser.getBirthday()));
                    appRankingDto.setVoiceSignatureSeconds(appUser.getVoiceSignatureSeconds());
                    //判断用户是否是贵族
                    if (appUser.getIsguizu() == 1) {
                        //是贵族
                        //根据贵族id  获取贵族图标
                        String medal = guizuCacheMap.get(appUser.getNobleid());
                        appRankingDto.setMedal(medal);
                    } else {
                        appRankingDto.setMedal("");
                    }
                    if(appUser.getVipEndDate() != null) {
                        appRankingDto.setIsVip(TimeUtils.afterNow(appUser.getVipEndDate())? 1: 0);
                    }
                    appRankingDto.setNobleid(appUser.getNobleid());
                    appRankingDto.setIsguizu(appUser.getIsguizu());
                    appRankingDtos.add(appRankingDto);
                }
            });
            return appRankingDtos;
        }
        return null;
    }


}
